﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StockCalc
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            App.LocationWindow2ScreenCenter(this);
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            tbGuben.Clear();
            tbNowLr.Clear();
            tbGrowLv.Text = "5"; 
            tbAfterLr.Clear();
            tb20Result.Clear();
            tb30Result.Clear();
            tb25Result.Clear();
            tb50Result.Clear();
            tb85Result.Clear();
            tbBeginLr.Clear();
            tbEndlr.Clear();
            tbYear.Text="3";
            tbGrowResult.Clear();
            tbEndMoney.Clear();
            tbMoneyResult.Clear();
            tbMoneyLv.Text = "15";
            tbMoneyYear.Text = "3";
            tbNowPrice.Clear();
            tb100Money.Clear();
            tbPerLr.Clear();
            tbPerLrlv.Clear();
            tb10Fh.Text="0";
            tbPerGuxi.Clear();
            tbPerWinLv.Clear();

            tbMoney3p.Clear();
            tbMoney5p.Clear();
            tballMoney.Clear();
            tb3yMoney.Clear();
            tb2yMoney.Clear();
            tb1yMoney.Clear();
            tbNowMoney.Text = "0";
        }

        private void BtnCalc_Click(object sender, RoutedEventArgs e)
        {
            string sGb = tbGuben.Text;
            string sNowlr = tbNowLr.Text;
            string sGrowLv = tbGrowLv.Text;
            try
            {
                double fGb = double.Parse(sGb);
                double fNowLr = double.Parse(sNowlr);
                double fGrowlv = double.Parse(sGrowLv);
                //3年后的利润
                double fAfterLr = fNowLr * Math.Pow(1+fGrowlv/100.0, 3.0);
                tbAfterLr.Text = $"{fAfterLr:0.00}"; //fAfterLr.ToString("0.00");

                double f20 = fAfterLr * 20  / 2.0 / fGb;
                tb20Result.Text = $"{f20:0.00}";                

                double f25 = fAfterLr * 25  / 2.0 / fGb;
                tb25Result.Text = $"{f25:0.00}";

                double f30 = fAfterLr * 30 * 1.5 / fGb;
                tb30Result.Text = $"{f30:0.00}";

                double f50 = fNowLr * 50 / fGb;
                tb50Result.Text = $"{f50:0.00}";

                //格老估值=当前利润*（8.5+2*增长率）
                double f85 = fNowLr * (8.5 + 2 * fGrowlv) / fGb;
                tb85Result.Text = $"{f85:0.00}";

                string sNowMoney = tbNowMoney.Text;
                double fMoney0 = double.Parse(sNowMoney);

                double dGrowlv = fGrowlv / 100.0;
                double fMoney1_lr = fNowLr * (1 + dGrowlv); //第1年利润折现12%，第1年利润 = 当前利润*(1+lv)
                double fMoney1 = fMoney1_lr / (1 + 0.12); //折现到现在 //
                tb1yMoney.Text = $"{fMoney1:0.00}";

                double fMoney2_lr = fMoney1_lr * (1 + dGrowlv);
                double fMoney2 = fMoney2_lr / Math.Pow(1 + 0.12, 2);
                tb2yMoney.Text = $"{fMoney2:0.00}";

                double fMoney3_lr = fMoney2_lr * (1 + dGrowlv);
                double fMoney3 = fMoney3_lr / Math.Pow(1 + 0.12, 3);
                tb3yMoney.Text = $"{fMoney3:0.00}";

                double fMoney4 = fMoney0 + fMoney1 + fMoney2 + fMoney3;
                tballMoney.Text = $"{fMoney4:0.00}";

                double gzhi3 = fMoney4 * 3 / fGb;
                double gzhi5 = fMoney4 * 5 / fGb;
                tbMoney3p.Text = $"{gzhi3:0.00}";
                tbMoney5p.Text = $"{gzhi5:0.00}";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        private void btnGrow_Click(object sender, RoutedEventArgs e)
        {
            string sBegin = tbBeginLr.Text;
            string sEnd = tbEndlr.Text;
            string sYear = tbYear.Text;
            try
            {
                double fBegin = double.Parse(sBegin);
                double fEnd = double.Parse(sEnd);
                int iYear = int.Parse(sYear);
                //格老估值=当前利润*（8.5+2*增长率）
                double f85 = Math.Pow(fEnd/fBegin,1.0/iYear)-1;
                tbGrowResult.Text = $"{f85*100:0.00}%";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnMoney_Click(object sender, RoutedEventArgs e)
        {
            string sEndMy = tbEndMoney.Text;
            string sMyLv = tbMoneyLv.Text;
            string sYear = tbMoneyYear.Text;
            try
            {
                double fEndMy = double.Parse(sEndMy);
                double fMyLv = double.Parse(sMyLv);
                int iYear = int.Parse(sYear);
                //格老估值=当前利润*（8.5+2*增长率）
                double f85 = fEndMy / Math.Pow(1 + fMyLv / 100.0, iYear);
                tbMoneyResult.Text = $"{f85:0.00}";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnWinLv_Click(object sender, RoutedEventArgs e)
        {
            string sPrice = tbNowPrice.Text;
            if (string.IsNullOrEmpty(sPrice))
            {
                sPrice = tb25Result.Text;
                tbNowPrice.Text = sPrice;
            }
            

            string s3Lr = tbAfterLr.Text;
            string sGb = tbGuben.Text;
            
            try
            {
                double f3Lr = double.Parse(s3Lr);
                double fGb = double.Parse(sGb);
                double fPerSy = f3Lr / fGb;
                tbPerLr.Text = $"{fPerSy:0.00}";                

                double fPrice = double.Parse(sPrice);
                double f100Money = 100 * fPrice; //100股成本
                tb100Money.Text = $"{f100Money:0.00}"; 

                double fPerlrLv = (fPerSy * 100) / f100Money; //100的收益/100股的成本=每股收益率
                tbPerLrlv.Text = $"{fPerlrLv*100:0.00}%"; 

                double f10fh = double.Parse(tb10Fh.Text);
                double f10fhxi = f10fh/10/fPrice;
                tbPerGuxi.Text = $"{f10fhxi*100:0.00}%";
                tbPerWinLv.Text = $"{(f10fhxi+ fPerlrLv) * 100:0.00}%"; //每股收益率+每股股息率=每股总收益率           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}